from django.db import models

import uuid


class DownloadTaskDetail(models.Model):
    """ 用户提交下载任务的基本信息表."""

    # [1] 任务类型(report ot table)
    TASK_TYPE_CHOICES = (
        ('table', '数据表'),
        ('report', '征信报告'),
    )
    DEFAULT_TASK_TYPE = 'table'

    # [2] 任务等级
    TASK_PRIORITY_CHOICES = (
        ('A', '一级'),
        ('B', '二级'),
        ('C', '三级'),
        ('D', '四级'),
        ('E', '五级')
    )
    DEFAULT_TASK_PRIORITY = 'C'

    # [3] 保存时长
    RETAIN_CHOICES = (
        (12, '12 Hours'),
        (18, '18 Hours'),
        (24, '01 Day'),
        (48, '02 Days'),
        (120, '05 Days'),
        (168, '07 Days'),
        (360, '15 Days'),
        (720, '30 Days')
    )
    DEFAULT_RETAIN = 12

    # [4] 加密方式
    ENCRYPT_CHOICES = (
        ('NO', '否'),
        ('MD5', 'MD5'),
        ('RSA', 'RSA'),
        ('SHA1', 'SHA1')
    )
    DEFAULT_ENCRYPT = 'NO'

    # [5] 抄送邮箱
    DEFAULT_MAIL_TO = 'zhoujin@slooptech.com'

    task_id = models.UUIDField(help_text='任务ID', primary_key=True, null=False, default=uuid.uuid4())
    task_name = models.CharField(help_text='任务名称', max_length=64)
    apply_time = models.DateTimeField(help_text='任务申请时间')
    task_type = models.CharField(help_text='任务类型', max_length=16, default=DEFAULT_TASK_TYPE)
    priority = models.CharField(help_text='任务等级', max_length=8, default=DEFAULT_TASK_PRIORITY)
    project = models.CharField(help_text='Odps 项目空间', max_length=64, blank=False)
    table = models.CharField(help_text='Odps 数据表名', max_length=64, blank=False)
    upload_file = models.FileField(help_text='用户上传订单文件', upload_to='tmp', blank=True, null=True)
    save_path = models.FilePathField(help_text='下载文件存储目录', max_length=256, null=True, allow_files=True)
    file_type = models.CharField(help_text='文件类型', max_length=8, default='csv', blank=True)
    delimiter = models.CharField(help_text='文本分隔符', max_length=4, default=',', null=True)
    is_encrypt = models.CharField(help_text='是否加密', max_length=8, default=DEFAULT_ENCRYPT, choices=ENCRYPT_CHOICES)
    limit = models.CharField(help_text='下载条数限制', max_length=16, default='all', blank=True)
    retain = models.IntegerField(help_text='文件保存时长', choices=RETAIN_CHOICES, default=DEFAULT_RETAIN)
    instruction = models.TextField(help_text='数据描述')
    purpose = models.CharField(help_text='下载使用目的', max_length=256)
    remark = models.TextField(help_text='额外备注信息')
    mail_to = models.CharField(help_text='抄送邮件', max_length=256, default=DEFAULT_MAIL_TO)
    table_meta = models.TextField(help_text='Odps 数据表的 Meta 信息', default='')
    create_time = models.DateTimeField(help_text='记录创建时间', auto_now=False, auto_now_add=True)
    update_time = models.DateTimeField(help_text='记录更新时间', auto_now=True, auto_now_add=False)

    class Meta:
        # label
        app_label = "ddm"

        # 抽象基类
        abstract = False

        # 数据表名称
        db_table = "download_task_detail"

        # 查询数据的排序字段
        get_latest_by = ['-update_time', 'task_id']

        # 由命令行工具管理(创建、迁徙)模型
        managed = True

        # 模型排序字段
        ordering = ['task_id']

        # admin show : verbose
        verbose_name = "任务基本信息"


class DownloadTaskResult(models.Model):
    """ 任务审批结果. """

    # 审批结果
    APPROVE_CHOICES = (
        ('Approve', '审批通过'),
        ('Deny', '审批拒绝'),
        ('Waiting', '等待审批'),
    )
    DEFAULT_APPROVE = 'Waiting'

    # 任务执行状态
    STATUS_CHOICES = (
        ('Success', '执行成功'),
        ('Failure', '任务失败'),
        ('Execute', '正在执行'),
        ('Waiting', '等待执行'),
    )
    DEFAULT_STATUS = 'Waiting'

    task = models.OneToOneField(DownloadTaskDetail, on_delete=models.CASCADE, primary_key=True, max_length=100)
    celery_task_id = models.UUIDField(help_text='Celery执行任务id', null=True, unique=True, max_length=100)
    username = models.CharField(help_text='申请人', max_length=64)
    apply_time = models.DateTimeField(help_text='申请时间', auto_now_add=True)
    task_name = models.CharField(help_text='任务名称', max_length=64)
    approver = models.CharField(help_text='审批人', max_length=64, default='--')
    approval = models.CharField('下载任务审批结果', max_length=16, choices=APPROVE_CHOICES, default=DEFAULT_APPROVE)
    reason = models.CharField(help_text='审批理由', max_length=256, default='未审批')
    status = models.CharField('任务执行状态', max_length=16, choices=STATUS_CHOICES, default=DEFAULT_STATUS)
    execute_info = models.CharField(help_text='执行下载任务返回信息', max_length=255, null=False, default='--')
    success_rate = models.CharField(help_text='执行成功率', max_length=12, default='0.0 %')
    receipt_mail = models.BooleanField(help_text='是否已经发送回执邮件', default=False)
    create_time = models.DateTimeField(help_text='首次创建时间', auto_now=False, auto_now_add=True)
    update_time = models.DateTimeField(help_text='记录更新时间', auto_now=True, auto_now_add=False)

    class Meta:
        # label
        app_label = "ddm"

        # 抽象基类
        abstract = False

        # 数据表名
        db_table = "download_task_result"

        # 查询任务排序字段
        get_latest_by = ['-create_time', 'approval', 'task_type', 'task_id']

        # 是否受 django  管理(创建、迁徙)模型
        managed = True

        # 模型排序字段
        ordering = ['task_id']

        # admin show : verbose
        verbose_name = "任务审批状态"
